import request from '@/utils/request'

export const teacherApi = {
  getTeacherList: () => {
    return request.get('/spoc/teacher/getTeacherList')
  },
  getTeacherById: (id) => {
    return request.get(`/spoc/teacher/getTeacher/${id}`)
  },
  updateTeacher: (data) => {
    return request.post('/spoc/teacher/updateTeacher', data)
  },
  saveTeacher: (data) => {
    return request.post('/spoc/teacher/saveTeacher', data)
  },
  deleteTeacher: (id) => {
    return request.delete(`/spoc/teacher/remove/${id}`)
  },
  getUid: (teacherId) => {
    return request.get(`/spoc/teacher/getUid/${teacherId}`)
  },
  getTeacherClasses: (teacherId) => {
    return request.get(`/spoc/tea-class/teacher/${teacherId}`)
  },
  getTeacherCourses: (teacherId) => {
    return request.get(`/spoc/teacher/${teacherId}/courses`, {
      params: {
        timestamp: Date.now()  // 防止缓存
      }
    })
  },
  getTeacherByUid: (userId) => {
    return request.get(`/spoc/teacher/getTeacherByUid/${userId}`)
  },
  getClassDetail: (teacherId, classId) => {
    return request.get(`/spoc/teacher-class-detail/${teacherId}/${classId}`, {
      params: {
        _t: Date.now() // 防止缓存
      }
    })
  },

  // 检查教学权限
  checkTeachingPermission(teacherId, courseId) {
    console.log('[API] 发起权限检查请求', { teacherId, courseId })
    return request.get('/spoc/teacher-class-detail/check-teaching-permission', {
      params: { teacherId, courseId }
    }).then(response => {
      console.log('[API] 权限检查原始响应:', response)
      const hasPermission = typeof response === 'boolean' ? response : 
                          response?.data === true
      console.log('[API] 解析后的权限:', hasPermission)
      return { data: hasPermission }
    }).catch(error => {
      console.error('[API] 权限检查错误:', error)
      return { data: false }
    })
  },

  // 获取学生课程进度
  getStudentProgress(teacherId, classId, studentId) {
    console.log('[API] 获取学生课程进度', { teacherId, classId, studentId })
    return request.get(`/spoc/teacher-class-detail/student-progress/${teacherId}/${classId}/${studentId}`)
      .then(response => {
        console.log('[API] 学生进度原始响应:', response)
        // 处理不同响应格式
        const progressData = Array.isArray(response) ? response : 
                           Array.isArray(response?.data) ? response.data : []
        console.log('[API] 处理后的进度数据:', progressData)
        return { data: progressData }
      })
      .catch(error => {
        console.error('[API] 获取学生进度错误:', error)
        return { data: [] }
      })
  },
  // 新增接口
  // 修正后（与后端Controller路径一致）
// 创建班级（与后端Controller路径一致）
  createClass: (data) => {
    return request.post('/spoc/tea-class', data)
  },
  
  // 添加课程到班级
  addCoursesToClass: (classId, courseIds) => {
    return request.post(`/spoc/tea-class/${classId}/courses`, courseIds)
  },
  
  // 新增：获取教师班级关系（仅classId）
  getTeacherClassRelations(teacherId) {
    return request.get(`/spoc/tea-class/teacher-relations/${teacherId}`)
  },
  
  // 新增：批量获取班级基本信息
  getClassesBasicInfo(classIds) {
    return request.post('/spoc/class/batch-basic-info', { classIds })
  },

  deleteClass(classId) {
    return request.delete(`/spoc/tea-class/${classId}`)
  },

  // 获取教师班级关系
  getTeacherClassRelations(teacherId) {
    return request({
      url: `/spoc/tea-class/teacher-relations/${teacherId}`,
      method: 'get'
    })
  },
  
  // 获取教师课程
  getTeacherCourses(teacherId) {
    return request({
      url: `/spoc/vod/course/teacherCourseList/${teacherId}`,
      method: 'get'
    })
  },
  
  // 获取班级课程
  getClassCourses(classId) {
    return request({
      url: `/api/spoc/cla/${classId}/courses`,
      method: 'get'
    })
  },
  
  // 添加课程到班级
  addCourseToClass(classId, courseId) {
    return request({
      url: `/api/spoc/cla/${classId}/add-course`,
      method: 'post',
      data: { courseId }
    })
  },
  
  // 更新班级信息
  updateClass(data) {
    return request({
      url: `/api/spoc/cla/${data.id}`,
      method: 'put',
      data: {
        className: data.className
      }
    })
  },

  addCourseToClass(classId, courseId) {
    return request({
      url: `/spoc/tea-class/${classId}/courses`,
      method: 'post',
      data: [courseId] // 注意这里需要传递数组
    })
  },

  removeStudentFromClass(classId, studentId) {
  return request({
    url: `/api/spoc/cla/${classId}/students/${studentId}`,
    method: 'delete'
  })
},
// 获取教师课程
  getTeacherCourses: (teacherId) => {
    return request.get(`/spoc/vod/course/teacher/${teacherId}`)
  },
   // 创建课程
  createCourse: (courseData) => {
    return request.post('/spoc/vod/course/create', courseData)
  },
  
  // 为课程添加章节
  addChapters: (courseId, chapters) => {
    return request.post(`/spoc/vod/course/${courseId}/chapters`, chapters)
  },

  // 在teacherApi中添加新方法
createCourseWithChapters: (courseData) => {
  return request({
    url: '/spoc/vod/course/create-with-chapters',
    method: 'post',
    data: courseData
  });
}
}